// 主题管理器
class ThemeManager {
    constructor() {
        this.currentTheme = 'default'
        this.themes = {
            default: {
                name: '默认主题',
                primary: '#1890ff',
                success: '#13ce66',
                warning: '#ffba00',
                danger: '#ff4949'
            },
            blue: {
                name: '蓝色主题',
                primary: '#1890ff',
                success: '#52c41a',
                warning: '#faad14',
                danger: '#ff4d4f'
            },
            green: {
                name: '绿色主题',
                primary: '#67C23A',
                success: '#85ce61',
                warning: '#E6A23C',
                danger: '#F56C6C'
            },
            dark: {
                name: '深色主题',
                primary: '#409EFF',
                success: '#67C23A',
                warning: '#E6A23C',
                danger: '#F56C6C'
            }
        }
    }

    // 初始化主题
    init() {
        const savedTheme = localStorage.getItem('theme') || 'default'
        this.setTheme(savedTheme)
    }

    // 设置主题
    setTheme(theme) {
        if (!this.themes[theme]) {
            console.warn(`主题 ${theme} 不存在`)
            return
        }

        this.currentTheme = theme
        localStorage.setItem('theme', theme)

        // 移除现有主题样式
        this.removeThemeStyles()

        // 应用新主题
        if (theme !== 'default') {
            this.loadThemeStyles(theme)
        }

        // 更新CSS变量
        this.updateCSSVariables(theme)
    }

    // 移除主题样式
    removeThemeStyles() {
        const existingLink = document.getElementById('theme-style')
        if (existingLink) {
            existingLink.remove()
        }
    }

    // 加载主题样式
    loadThemeStyles(theme) {
        const link = document.createElement('link')
        link.id = 'theme-style'
        link.rel = 'stylesheet'
        link.href = `/src/assets/styles/theme-${theme}.scss`
        document.head.appendChild(link)
    }

    // 更新CSS变量
    updateCSSVariables(theme) {
        const themeConfig = this.themes[theme]
        const root = document.documentElement

        // 设置CSS变量
        root.style.setProperty('--el-color-primary', themeConfig.primary)
        root.style.setProperty('--el-color-success', themeConfig.success)
        root.style.setProperty('--el-color-warning', themeConfig.warning)
        root.style.setProperty('--el-color-danger', themeConfig.danger)

        // 如果是深色主题，设置深色模式变量
        if (theme === 'dark') {
            root.style.setProperty('--el-bg-color', '#1a1a1a')
            root.style.setProperty('--el-bg-color-page', '#1a1a1a')
            root.style.setProperty('--el-text-color-primary', '#ffffff')
            root.style.setProperty('--el-text-color-regular', '#e0e0e0')
            root.style.setProperty('--el-border-color', '#4a4a4a')
        } else {
            // 重置为默认值
            root.style.setProperty('--el-bg-color', '#ffffff')
            root.style.setProperty('--el-bg-color-page', '#f2f3f5')
            root.style.setProperty('--el-text-color-primary', '#303133')
            root.style.setProperty('--el-text-color-regular', '#606266')
            root.style.setProperty('--el-border-color', '#dcdfe6')
        }
    }

    // 获取当前主题
    getCurrentTheme() {
        return this.currentTheme
    }

    // 获取主题配置
    getThemeConfig(theme) {
        return this.themes[theme] || this.themes.default
    }

    // 获取所有主题
    getAllThemes() {
        return this.themes
    }
}

// 创建全局主题管理器实例
const themeManager = new ThemeManager()

export default themeManager 